package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.*
import com.example.onlineeducation.repository.StudyRepository
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.collectLatest

class StudyContentVM : BaseViewModel() {
    private val mStudyRepository: StudyRepository by lazy { StudyRepository(mApiService) }

    private val _mGetChapterListDataModel = MutableLiveData<YcResult<List<ChapterEntity>?>>()
    val mGetChapterListDataModel: LiveData<YcResult<List<ChapterEntity>?>> = _mGetChapterListDataModel
    fun getChapterList(projectId: String, topicId: String) = ycLaunch {
        mStudyRepository.getChapterList(projectId, topicId).ycHasLoading().collectLatest {
            _mGetChapterListDataModel.postValue(it)
        }
    }

    private val _mPushStudyLog = MutableLiveData<YcResult<Any?>>()
    val mPushStudyLog: LiveData<YcResult<Any?>> = _mPushStudyLog

    fun pushStudyLog(projId: String, times: String, topicId: String) = ycLaunch {
        mStudyRepository.pushStudyLog(projId, times, topicId).ycHasLoading().collectLatest { result ->
            _mPushStudyLog.postValue(result)
        }
    }


    private val _mStudyTimeLoop = MutableLiveData<Boolean>()
    val mStudyTimeLoop: LiveData<Boolean> = _mStudyTimeLoop

    /*设置项目全局项目信息*/
    fun studyTimeLoop(isStart: Boolean) {
        _mStudyTimeLoop.postValue(isStart)
    }


    /**
     * 练习-题目列表
     */
    private val _mPractiseQuestionList = MutableLiveData<YcResult<List<ProblemEntity>?>>()
    val mPractiseQuestionList: LiveData<YcResult<List<ProblemEntity>?>> = _mPractiseQuestionList

    fun getPractiseQuestionList(chapterId: String) = ycLaunch {
        mStudyRepository.getPractiseQuestionList(chapterId).ycHasLoading().collectLatest { result ->
            _mPractiseQuestionList.postValue(result)
        }
    }


    /**
     * 练习-题目列表
     */
    private val _mPractiseSubmitResult = MutableLiveData<YcResult<List<String>?>>()
    val mPractiseSubmitResult: LiveData<YcResult<List<String>?>> = _mPractiseSubmitResult

    fun submitPractiseOptions(
        proId: String,
        topicId: String,
        chapterId: String,
        questionId: String,
        optionList: List<OptionEntity>
    ) = ycLaunch {
        mStudyRepository.submitPractiseOptions(proId, topicId, chapterId, questionId, optionList).ycHasLoading().collectLatest { result ->
            _mPractiseSubmitResult.postValue(result)
        }
    }

}
